package com.jgzj.example.export.controller;

import com.jgzj.example.export.model.export.ExportReqDto;
import com.jgzj.example.export.service.ExportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;

/**
 * @description:
 * @author: 子木
 * @since: 2021/05/16
 */
@RestController
@CrossOrigin
@RequestMapping("/export")
public class ExportController {

    @Autowired
    private ExportService exportService;

    @RequestMapping(value = "/download", produces = {"application/vnd.ms-excel;charset=UTF-8"})
    public ResponseEntity<byte[]> downloadData(@RequestBody ExportReqDto reqDto) throws UnsupportedEncodingException {
        byte[] content = exportService.download(reqDto);
        // 文件下载
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Content-Disposition", "attachment;filename=" +
                new String((reqDto.getSheetName() + ".xls").getBytes("UTF-8"), "iso-8859-1"));
        return new ResponseEntity<>(content, httpHeaders, HttpStatus.OK);
    }

}